package com.education.api.controller.admin.system;

import com.education.business.service.system.ExamTaskService;
import com.education.common.utils.Result;
import com.education.model.dto.ExamTaskDto;
import com.education.model.request.PageParam;
import com.education.model.subproject.ExamTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/exam")
public class ExamTaskController {
    @Autowired
    private ExamTaskService examTaskService;

    @RequestMapping(value = "/getExamTaskList",method = RequestMethod.GET)
    public Result getExamTaskAll(PageParam pageParam,ExamTask examTask){
        return Result.success(examTaskService.selectExamTaskInfoList(pageParam,examTask));
    }
    //监考端显示任务名称、考试时间
    @GetMapping(value = "/selectByIdExamTask")
    public Result selectByIdExamTask(ExamTask examTask){
        return Result.success(examTaskService.selectExamById(examTask));
    }
    @RequestMapping(value = "/addExamTask",method = RequestMethod.GET)
    public Result addExamTask( ExamTask examTask){
        System.out.println(examTask.getStartTime());
        return Result.success(examTaskService.save(examTask));
    }
    @GetMapping(value = "/updateExamTaskStatus")
    public Result updateExamTaskStatus( ExamTaskDto examTaskDto){
        System.out.println(examTaskDto.getStatu()+examTaskDto.getId());
        return Result.success(examTaskService.editStatus(examTaskDto));
    }
    @GetMapping(value = "/updateExamTask")
    public Result updateExamTask( ExamTask examTask){
        return Result.success(examTaskService.updateById(examTask));
    }

    @GetMapping(value = "/deleteExamTaskById")
    public Result deleteExamTaskById(@RequestParam("id") Integer id){
        return Result.success(examTaskService.deleteById(id));
    }
    //只需要加上下面这段即可，注意不能忘记注解
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {

        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }



}
